package com.etcsoft.pagedesigner.view;

import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.jface.viewers.ViewerFilter;

import com.etcsoft.pagedesigner.util.StringMatcher;

/**
 * @author korman21@gmail.com
 *
 */
public abstract class BaseProjectFilter extends ViewerFilter {
    private StructuredViewer viewer;
    private String pattern = "";
    private StringMatcher matcher;

    public BaseProjectFilter(StructuredViewer viewer) {
        this.viewer = viewer;
    }

    public String getPattern() {
        return pattern;
    }

    public void setPattern(String newPattern) {
        boolean filtering = matcher != null;

        if (newPattern != null && newPattern.trim().length() > 0) {
            pattern = newPattern;
            matcher = new StringMatcher(pattern, false, false);
            if (!filtering) {
                viewer.addFilter(this);
            } else {
                viewer.refresh();
            }
        } else {
            pattern = "";
            matcher = null;
            if (filtering) {
                viewer.removeFilter(this);
            }
        }
    }

    protected StringMatcher getMatcher() {
        return matcher;
    }
}
